package edu.chalmers.sdx;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.LinkedList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import edu.chalmers.targine.Mesh;
import edu.dhbw.andar.util.Model;

public class GameGrid extends Mesh{
	private List<GridCell> cells;

//	private float[][] colors = { // Colors of the 6 faces
//	{ 1.0f, 0.5f, 0.0f, 1.0f }, // 0. orange
//			{ 1.0f, 0.0f, 1.0f, 1.0f }, // 1. violet
//			{ 0.0f, 1.0f, 0.0f, 1.0f }, // 2. green
//			{ 0.0f, 0.0f, 1.0f, 1.0f }, // 3. blue
//			{ 1.0f, 0.0f, 0.0f, 1.0f }, // 4. red
//			{ 1.0f, 1.0f, 0.0f, 1.0f } // 5. yellow
//	};

	public GameGrid(float size) {
		cells = new LinkedList<GridCell>();
		for(int i = -5; i < 5; i += 2){
			for(int j = -5; j < 5; j += 2){				
				float[] vertices = new float[]{
						i*size, j*size, 0.0f, //left-bottom
						(i + 2)*size, j*size, 0.0f,  //right-bottom
						i*size, (j + 2)*size, 0.0f,  //left-top
						(i + 2)*size, (j + 2)*size, 0.0f,    //right-top
						
						i*size, j*size, 0.0f, //left-bottom
						i*size, (j + 2)*size, 0.0f,  //left-top
						(i + 2)*size, j*size, 0.0f,  //right-bottom
						(i + 2)*size, (j + 2)*size, 0.0f    //right-top
				};
				
				GridCell cell = new GridCell(vertices);
				
				cells.add(cell);
			}
		}
	}
	
	public void init(GL10 gl){
		
	}

	// Draw the shapes
//	public void draw(GL10 gl, int nextGridCell) {
	public void draw(GL10 gl) {
		//TODO: Use nextGridCell to fill the correct GridCell with color.
		for(GridCell cell : cells){
			cell.draw(gl);
		}
	}

	private class GridCell {
		public boolean isHitByRay = false;
		
		private FloatBuffer vertexBuffer; // Buffer for vertex-array

		// Constructor - Set up the buffers
		public GridCell(float[] vertices) {
			// Setup vertex-array buffer. Vertices in float. A float has 4 bytes
			ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
			vbb.order(ByteOrder.nativeOrder()); // Use native byte order
			vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
			vertexBuffer.put(vertices); // Copy data into buffer
			vertexBuffer.position(0); // Rewind
		}

		// Draw the shape
		public void draw(GL10 gl) {
			gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f);
			gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise orientation
			gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
			gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display)

			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

			// Render all the faces
//			for (int face = 0; face < numFaces; face++) {
				// Set the color for each of the faces
//				gl.glColor4f(colors[face][0], colors[face][1], colors[face][2],
//						colors[face][3]);
			
			// Set the color for each of the faces
				// Draw the primitive from the vertex-array directly
//				gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, face * 4, 4);
			
			// Draw the primitive from the vertex-array directly
			if(isHitByRay){
				gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
			}
			gl.glDrawArrays(GL10.GL_LINES, 0, 8);
			gl.glLineWidth(5);
//			}
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glDisable(GL10.GL_CULL_FACE);
		}
	}

	@Override
	public Model getModel() {
		return null;
	}

	@Override
	public void update() {
	}

	@Override
	public boolean isFinalized() {
		return true;
	}

	@Override
	public boolean hasModel() {
		return true;
	}

	@Override
	public void finalize() {
	}
}